var $table = $('#userRoleTab');

$(function () {
    initTable();

    $("#addBut").click(function () {
        initRoleEdit(null);
    });
});

function initTable() {
    $table.bootstrapTable({
        url: '../role/getRolesPage.htm',
        dataType: "json",
        pagination: true, //分页
        striped: true,
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        columns: [
            {
                title: "角色名称",
                field: "roleName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "角色描述",
                field: "descr",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "操作",
                field: "id,descr",
                align: 'center',
                formatter: function (value, row, index) {
                	if(row.roleName == 'root' || row.roleName == 'admin'){
                		var a = '<shiro:hashRole name = "root"> <button class="btn btn-sm btn-primary" type="button" onclick="showConsume(\'' + row.id + '\',\'' + row.roleName + '\')"><i class="fa fa-edit"></i> 权限管理</button> </shiro:hashRole>';
                		return a;
                	}else{
                		var p = '<button class="btn btn-sm btn-primary" type="button" onclick="showConsume(\'' + row.id + '\',\'' + row.roleName + '\')"><i class="fa fa-edit"></i> 权限管理</button> ';
                    	var e = '<button class="btn btn-sm btn-info" type="button" onclick="initRoleEdit(\'' + row.id + '\')"><i class="fa fa-edit"></i> 编辑</button> ';
                    	var d = '<button class="btn btn-sm btn-danger" type="button" onclick="deleteUserRole(\'' + row.id + '\',\'' + row.descr + '\')"><i class="fa fa-trash-o"></i> 删除</button> ';
                		return e + p + d;
                	}
                }
            }
        ]
    });
}

function deleteUserRole(roleId, roleName) {
    layer.confirm('你确定要删除【' + roleName + '】角色吗？', {
        btn: ['删除', '取消'] //按钮
    }, function () {
        //删除角色
        $.ajax({
            url: "../role/delRoleInfo.htm",
            cache: false,
            type: "get",
            dataType: "json",
            data: {id: roleId},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: '../role/getRolesPage.htm'});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}

//编辑角色
function initRoleEdit(roleId) {
    var title;
    if (roleId){
        title = '编辑权限';
    }else{
        title = '新增角色';
    }
    layer.open({
        type: 2,
        title: title,
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area : ['500px' , '400px'],
        content: '../role/updateUserRole.htm?id='+roleId,
        success: function(layero, index){
        }
    });
}

//权限配置
function showConsume(roleId,roleName){
    layer.open({
        type: 2,
        title: '权限配置',
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area : ['270px' , '520px'],
        content: '/doublesys/sysrole_permission.htm?id='+roleId+'&roleName='+roleName,
        success: function(layero, index){}
    });
}
